package com.chengxusheji.dao;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.chengxusheji.domain.BusinessCompany;

@Service @Transactional
public class BusinessCompanyDAO {

	@Resource SessionFactory factory;
    /*每页显示记录数目*/
    private final int PAGE_SIZE = 10;

    /*保存查询后总的页数*/
    private int totalPage;
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
    public int getTotalPage() {
        return totalPage;
    }

    /*保存查询到的总记录数*/
    private int recordNumber;
    public void setRecordNumber(int recordNumber) {
        this.recordNumber = recordNumber;
    }
    public int getRecordNumber() {
        return recordNumber;
    }

    
    private String errMessage; 
	public String getErrMessage() {
		return errMessage;
	}
	public void setErrMessage(String errMessage) {
		this.errMessage = errMessage;
	}
	
    /*验证用户登录*/
	@Transactional(propagation=Propagation.NOT_SUPPORTED)  //不需要事务管理的(只查询的)方法
	public boolean CheckLogin(String userName,String password) { 
		Session s = factory.getCurrentSession(); 

		BusinessCompany db_businessCompany = (BusinessCompany)s.get(BusinessCompany.class, userName);
		if(db_businessCompany == null) { 
			this.errMessage = " 账号不存在 ";
			System.out.print(this.errMessage);
			return false;
		} else if( !db_businessCompany.getSjmm().equals(password)) {
			this.errMessage = " 密码不正确! ";
			System.out.print(this.errMessage);
			return false;
		}
		
		return true;
	}
	
	
    /*添加BusinessCompany信息*/
    public void AddBusinessCompany(BusinessCompany businessCompany) throws Exception {
    	Session s = factory.getCurrentSession();
    	s.save(businessCompany);
    }

    /*查询BusinessCompany信息*/
    @Transactional(propagation=Propagation.NOT_SUPPORTED)
    public ArrayList<BusinessCompany> QueryBusinessCompanyInfo(String sjad,String sjmc,String sjlxr,Integer shzt,int currentPage) { 
    	Session s = factory.getCurrentSession();
    	String hql = "From BusinessCompany businessCompany where 1=1";
    	if(!sjad.equals("")) hql = hql + " and businessCompany.sjad like '%" + sjad + "%'";
    	if(!sjmc.equals("")) hql = hql + " and businessCompany.sjmc like '%" + sjmc + "%'";
    	if(!sjlxr.equals("")) hql = hql + " and businessCompany.sjlxr like '%" + sjlxr + "%'";
    	if(shzt != null) hql = hql + " and businessCompany.shzt=" + shzt;
    	Query q = s.createQuery(hql);
    	/*计算当前显示页码的开始记录*/
    	int startIndex = (currentPage-1) * this.PAGE_SIZE;
    	q.setFirstResult(startIndex);//从第一条记录开始
    	q.setMaxResults(this.PAGE_SIZE);//取出一页数据
    	List businessCompanyList = q.list();
    	return (ArrayList<BusinessCompany>) businessCompanyList;
    }

    @Transactional(propagation=Propagation.NOT_SUPPORTED)
    public ArrayList<BusinessCompany> QueryBusinessCompanyInfo(String sjad,String sjmc,String sjlxr) { 
    	Session s = factory.getCurrentSession();
    	String hql = "From BusinessCompany businessCompany where 1=1";
    	if(!sjad.equals("")) hql = hql + " and businessCompany.sjad like '%" + sjad + "%'";
    	if(!sjmc.equals("")) hql = hql + " and businessCompany.sjmc like '%" + sjmc + "%'";
    	if(!sjlxr.equals("")) hql = hql + " and businessCompany.sjlxr like '%" + sjlxr + "%'";
    	Query q = s.createQuery(hql);
    	List businessCompanyList = q.list();
    	return (ArrayList<BusinessCompany>) businessCompanyList;
    }

    @Transactional(propagation=Propagation.NOT_SUPPORTED)
    public ArrayList<BusinessCompany> QueryAllBusinessCompanyInfo() {
        Session s = factory.getCurrentSession();
        String hql = "From BusinessCompany";
        Query q = s.createQuery(hql);
        List businessCompanyList = q.list();
        return (ArrayList<BusinessCompany>) businessCompanyList;
    }

    @Transactional(propagation=Propagation.NOT_SUPPORTED)
    public ArrayList<BusinessCompany> QueryAllBusinessCompanyInfoCheckOk() {
        Session s = factory.getCurrentSession();
        String hql = "From BusinessCompany businessCompany where businessCompany.shzt=1";
        Query q = s.createQuery(hql);
        List businessCompanyList = q.list();
        return (ArrayList<BusinessCompany>) businessCompanyList;
    }
    
    /*计算总的页数和记录数*/
    @Transactional(propagation=Propagation.NOT_SUPPORTED)
    public void CalculateTotalPageAndRecordNumber(String sjad,String sjmc,String sjlxr,Integer shzt) {
        Session s = factory.getCurrentSession();
        String hql = "From BusinessCompany businessCompany where 1=1";
        if(!sjad.equals("")) hql = hql + " and businessCompany.sjad like '%" + sjad + "%'";
        if(!sjmc.equals("")) hql = hql + " and businessCompany.sjmc like '%" + sjmc + "%'";
        if(!sjlxr.equals("")) hql = hql + " and businessCompany.sjlxr like '%" + sjlxr + "%'";
        if(shzt != null) hql = hql + " and businessCompany.shzt=" + shzt;
        Query q = s.createQuery(hql);
        List businessCompanyList = q.list();
        recordNumber = businessCompanyList.size();
        int mod = recordNumber % this.PAGE_SIZE;
        totalPage = recordNumber / this.PAGE_SIZE;
        if(mod != 0) totalPage++;
    }

    /*根据主键获取对象*/
    @Transactional(propagation=Propagation.NOT_SUPPORTED)
    public BusinessCompany GetBusinessCompanyBySjad(String sjad) {
        Session s = factory.getCurrentSession();
        BusinessCompany businessCompany = (BusinessCompany)s.get(BusinessCompany.class, sjad);//立即去执行数据库查询
        return businessCompany;
    }

    /*更新BusinessCompany信息*/
    public void UpdateBusinessCompany(BusinessCompany businessCompany) throws Exception {
        Session s = factory.getCurrentSession();
        s.update(businessCompany);
    }

    /*删除BusinessCompany信息*/
    public void DeleteBusinessCompany (String sjad) throws Exception {
        Session s = factory.getCurrentSession(); //
        Object businessCompany = s.load(BusinessCompany.class, sjad);//这句代码不会去执行数据库查询，只有用到BusinessCompany时才会去执行数据库查询。
        s.delete(businessCompany);
    }

}
